﻿using System.Drawing;

namespace SilverlightTetris_M10CDE.Web
{
    /// <summary>
    /// Defines a shape object
    /// </summary>
    public abstract class Shape
    {
        protected Block[] shapeBlock;//holds the blocks of the shape
        protected char orientation;//current rotational position of the shape

        /// <summary>
        /// get X position of block
        /// </summary>
        /// <param name="aBlock"></param>
        /// <returns></returns>
        public int getX(int aBlock)
        {
            return shapeBlock[aBlock].xPosition;
        }

        /// <summary>
        /// get Y position of block
        /// </summary>
        /// <param name="aBlock"></param>
        /// <returns></returns>
        public int getY(int aBlock)
        {
            return shapeBlock[aBlock].yPosition;
        }

        /// <summary>
        /// get color of shape (purple is default)
        /// </summary>
        /// <returns></returns>
        public virtual Color getColour() //virtual allows method overriding
        {
            return Color.Purple;
        }

        /// <summary>
        /// Moves block down
        /// </summary>
        public void gravityMove()
        {
            foreach (Block i in shapeBlock)
            {
                i.moveBlock('S');
            }
        }

        /// <summary>
        /// get block position
        /// </summary>
        /// <param name="block"></param>
        /// <returns></returns>
        public Block GetBlock(int block)
        {
            return shapeBlock[block];
        }

        /// <summary>
        /// move block left
        /// </summary>
        public void moveLeft()
        {
            foreach (Block i in shapeBlock)
            {
                i.moveBlock('W');
            }
        }

        /// <summary>
        /// move block right
        /// </summary>
        public void moveRight()
        {
            foreach (Block i in shapeBlock)
            {
                i.moveBlock('E');
            }
        }

        /// <summary>
        /// rotate shape
        /// </summary>
        public void RotateShape()
        {
            switch (orientation)
            {
                case 'n':
                    SetRotationEast();
                    break;
                case 'e':
                    SetRotationSouth();
                    break;
                case 's':
                    SetRotationWest();
                    break;
                case 'w':
                    SetRotationNorth();
                    break;
            }
        }

        /// <summary>
        /// undo rotation (if cant rotate)
        /// </summary>
        public void UndoRotateShape()
        {
            switch (orientation)
            {
                case 'n':
                    SetRotationWest();
                    break;
                case 'e':
                    SetRotationNorth();
                    break;
                case 's':
                    SetRotationEast();
                    break;
                case 'w':
                    SetRotationSouth();
                    break;
            }
        }

        /// <summary>
        /// add all shape block positions into a jagged array
        /// </summary>
        /// <returns></returns>
        public int[][] ToArray()
        {
            int[][] array = new int[4][];

            array[0] = new int[] { shapeBlock[0].xPosition, shapeBlock[0].yPosition };
            array[1] = new int[] { shapeBlock[1].xPosition, shapeBlock[1].yPosition };
            array[2] = new int[] { shapeBlock[2].xPosition, shapeBlock[2].yPosition };
            array[3] = new int[] { shapeBlock[3].xPosition, shapeBlock[3].yPosition };

            return array;
        }

        protected virtual void SetRotationNorth()
        {

        }

        protected virtual void SetRotationEast()
        {

        }

        protected virtual void SetRotationSouth()
        {

        }

        protected virtual void SetRotationWest()
        {

        }
    }
}